<template>
	<view class="page">
		<!---logoflex布局开始-->
		<view class="flex flex-direction flex-wrap align-center justify-center benben-flex-layout codeLogin_flex_0">
			<image class='codeLogin_fd0_0' mode="aspectFit" :src='STATIC_URL+"2.png"'></image>
		</view>
		<!---logoflex布局结束-->
		<!---flex布局flex布局开始-->
		<view class="flex flex-direction flex-wrap align-stretch benben-flex-layout codeLogin_flex_1">
			<view class='flex flex-wrap align-center codeLogin_fd1_0'>
				<image class='codeLogin_fd1_0_c0' mode="aspectFit" :src='STATIC_URL+"4.png"'></image>
				<input class='flex-sub codeLogin_fd1_0_c1' type="number" placeholder="请输入手机号" confirm-type="done"
					:maxlength="11" placeholder-style="color:rgba(153, 153, 153, 1);font-size:32rpx" v-model="mobile" />
			</view>
			<view class='flex flex-wrap align-center codeLogin_fd1_1'>
				<image class='codeLogin_fd1_1_c0' mode="aspectFit" :src='STATIC_URL+"6.png"'></image>
				<input class='flex-sub codeLogin_fd1_1_c1' type="number" placeholder="请输入验证码" confirm-type="done"
					:maxlength="6" placeholder-style="color:rgba(153, 153, 153, 1);font-size:32rpx" v-model="code" />
				<benben-send-verification-code class='flex  codeLogin_fd1_1_c2' after-text='后重新获取' before-text='获取验证码'
					type='3' :phone="mobile"></benben-send-verification-code>
			</view>
			<text class='self-end codeLogin_fd1_2' @tap.stop="handleJumpDiy" data-type="redirectTo"
				:data-url="`/pages/tabBar/login/login`">密码登录</text>
			<button class='codeLogin_fd1_3' @tap.stop="getcodeLogin()">登录</button>
			<view class='flex flex-wrap align-center justify-center codeLogin_fd1_4'>
				<text class='codeLogin_fd1_4_c0' @tap.stop="handleJumpDiy" data-type="redirectTo"
					:data-url="`/pages/denglu/register/register`">注册账号 </text>
				<view class='flex flex-wrap align-center codeLogin_fd1_4_c1'>
				</view>
				<text class='codeLogin_fd1_4_c2' @tap.stop="handleJumpDiy" data-type="redirectTo"
					:data-url="`/pages/denglu/forgetPassword/forgetPassword`">忘记密码</text>
			</view>
		</view>
		<!---flex布局flex布局结束-->
		<view class="flex flex-direction flex-wrap align-center benben-position-layout flex codeLogin_flex_2" @tap="wxLogin" v-if="status == 1">
			<view class='flex flex-wrap align-center'>
				<view class='flex flex-wrap align-center codeLogin_fd2_0_c0'>
				</view>
				<text class='codeLogin_fd2_0_c1'>快捷登录</text>
				<view class='flex flex-wrap align-center codeLogin_fd2_0_c2'>
				</view>
			</view>
			<image class='codeLogin_fd2_1' mode="aspectFit" :src='STATIC_URL+"5.png"'></image>

		</view>
		<view :style="{height: '237rpx'}"></view>

	</view>
</template>
<script>
	import {
		validate
	} from '@/common/utils/validate.js'
	export default {
		components: {},

		data() {
			return {
				"mobile": "",
				"code": "",
				"token": "123123123123123",
				"User": {
					"userinfo": {
						"user_token": "",
						"id": "",
						"head_img": "",
						"user_name": "",
						"user_nickname": "",
						"user_email": "",
						"sex": "",
						"user_type": "",
						"mobile": "",
						"birthday": "",
						"address": "",
						"address_code": "",
						"client_id": "",
						"invite_code": "",
						"is_finger": "",
						"autograph": ""
					}
				},
				"status": 0
			};
		},
		computed: {

		},
		watch: {},
		onLoad(options) {

		},
		onUnload() {

		},
		onReady() {

		},
		onShow() {
			this.isHide();
		},
		onHide() {

		},
		onResize() {

		},
		onPullDownRefresh() {

		},
		onReachBottom(e) {

		},
		onPageScroll(e) {

		},
		methods: {
			// 隐藏微信登录
			isHide() {
				this.$api.post(global.apiUrls.post6363619f7fd72, {
					
				}).then(res => {
					if (res.data.code == 1) {
						this.status = res.data.data.status;
					}
				})
			},
			//验证码登录
			async getcodeLogin() {
				if (!validate(this.mobile, 'require')) {
					this.$message.info('手机号不能为空');
					return false;
				}
				if (!validate(this.mobile, 'phone')) {
					this.$message.info('手机号格式不正确');
					return false;
				}
				if (!validate(this.code, 'require')) {
					this.$message.info('验证码不能为空');
					return false;
				}
				if (!validate(this.code, 'captcha')) {
					this.$message.info('验证码格式不正确');
					return false;
				}
				//请求方法
				//数据验证

				let UserData = await this.$api.post(global.apiUrls.post5c78dca45ebc1, {
					mobile: this.mobile,
					code: this.code,
					type: '3'
				});
				if (UserData.data.code != 1) {
					this.$message.info(UserData.data.msg);
					return
				}
				let Userinfo = UserData.data;
				this.User = Userinfo.data
				this.$store.commit('updateUserInfo', this.User.userinfo)
				this.$store.commit('saveToken', this.User.userinfo.user_token)
				this.$urouter.switchTab(`/pages/tabBar/index/index`);
			},
			/**
			 * @event 微信登陆
			 */
			wxLogin() {
				let _this = this;
				// if (!this.check) return this.$message.info('请阅读并同意用户协议');
				// #ifdef APP-PLUS
				// 如果是app环境打开微信
				if (
					plus.runtime.isApplicationExist({
						pname: "com.tencent.mm",
						action: "weixin://",
					})
				) {
					uni.getProvider({
						service: "oauth",
						success: function(res) {
							console.log(res);
							// 如果支持微信等，就执行 wx_login_fn 方法
							if (~res.provider.indexOf("weixin")) {
								_this.wx_login_fn();
							}
						},
						fail: function(err) {
							// api错误是打印错误
							console.log(err);
						},
					});
				} else {
					// 调起失败时提醒用户
					this.$message.info("请安装微信后再使用此功能");
				}
				// #endif
			},
			/**
			 * @function 微信授权登录
			 */
			wx_login_fn() {
				let _this = this;
				/**
				 * @desc uniapp的登陆api
				 * @param {String} provider - 登录服务提供商
				 * @param {login~success} loginRes - 接口调用成功时的回调函数
				 */
				uni.login({
					provider: "weixin",
					// onlyAuthorize: true, // 微信登录仅请求授权认证
					/**
					 * @callback login~success
					 * @param {Object} loginRes - api返回来的信息
					 */
					success: function(loginRes) {
						console.log(loginRes);
						/**
						 * @desc 获取用户信息
						 * @param {String} provider - 登录服务提供商
						 * @param {Funtion} success - 接口调用成功时的回调函数
						 */
						uni.getUserInfo({
							provider: "weixin",
							/**
							 * @callback success
							 * @param {Object} infoRes - api返回来的信息
							 */
							success: function(infoRes) {
								// 显示加载状态
								uni.showLoading({
									title: '加载中...',
								});
								/**
								 * @let {String} nickName - 结构出来的变量昵称
								 * @let {String} avatarUrl - 解构出来的头像链接
								 * @let {String} gender - 结构出来的性别
								 */
								let {
									nickName,
									avatarUrl,
									gender
								} = infoRes.userInfo;
								// #ifdef APP-PLUS
								let client_id = "client_id";
								//  @const {Number} appSystem - 判断设备类型 1：ios；2：安卓；
								let appSystem = uni.getSystemInfoSync().platform == "ios" ? 1 : 2;
								/**
								 * @description 第三方登录请求接口
								 */
								_this.$api
									.post(global.apiUrls.post5d7660a421e69, {
										type: 1,
										wx_unionid: infoRes.userInfo.unionId,
										qq_unionid: "",
										wb_unionid: "",
										client_id: client_id || "",
										appSystem: appSystem || "",
										nickname: nickName,
										avatar: avatarUrl,
										gender: gender,
									})
									.then((res) => {
										console.log(res);
										// 保存用户信息
										global.tempUserLoginInfo = infoRes.userInfo;
										// 关闭加载效果
										uni.hideLoading();
										if (res.data.code == 1) {
											_this.$message.info(res.data.msg);
											/**
											 * @description 向Vux发送用户信息保存
											 * @param {String} updateUserInfo - 调用的方法名称
											 * @param {Object} res.data.data.userinfo - 用户信息
											 */
											_this.$store.commit(
												"updateUserInfo",
												res.data.data.userinfo
											);
											global.token = res.data.data.userinfo.user_token;
											uni.setStorageSync('USER_TOKEN', res.data.data.userinfo.user_token);
											setTimeout(() => {
												uni.switchTab({
													url: '/pages/tabBar/index/index'
												})
											}, 800);
										} else if (res.data.code == -999) {
											/**
											 * @description 清除当前页面，跳转到绑定手机页面
											 */
											_this.$urouter.redirectTo({
												url: "/pages/denglu/bindingPhone/bindingPhone",
												params: {
													unionId: infoRes.userInfo.unionId,
													openId: infoRes.userInfo.openId,
													sex: infoRes.userInfo.gender,
													nickName: infoRes.userInfo
														.nickName,
													avatarUrl: infoRes.userInfo
														.avatarUrl,
													authType: "wx_unionid",
												},
											});
											// 点击手机号账号登录按钮
										} else if (res.code == -1) {
											_this.$message.info(res.data.msg);
											return;
										}
									})
									.catch((err) => {
										uni.hideLoading();
										console.log(err);
									});
								// #endif
								console.log(infoRes.userInfo.unionId, "app微信登录");
			
							},
							fail: function(err) {
								uni.hideLoading();
								console.log(err);
							}
						});
					},
					fail: function(err) {
						uni.hideLoading();
						console.log(err);
					},
				});
			},
		}
	};
</script>
<style lang="scss" scoped>
	.codeLogin_fd2_0_c0 {
		background: rgba(238, 238, 238, 1);
		background-size: 100% !important;
		background-size: 100% auto !important;
		height: 1rpx;
		width: 157rpx;
	}

	.codeLogin_fd2_0_c1 {
		line-height: 32rpx;
		font-size: 26rpx;
		font-weight: 400;
		color: rgba(153, 153, 153, 1);
		padding: 0rpx 7rpx 0rpx 7rpx;
	}

	.codeLogin_fd2_0_c2 {
		background: rgba(238, 238, 238, 1);
		background-size: 100% !important;
		background-size: 100% auto !important;
		height: 1rpx;
		width: 157rpx;
	}

	.codeLogin_fd2_1 {
		width: 48rpx;
		height: 48rpx;
		margin: 40rpx 0rpx 0rpx 0rpx;
	}

	.codeLogin_flex_2 {
		background: #fff;
		background-size: 100% !important;
		width: 750rpx;
		height: 237rpx;
		overflow: hidden;
		z-index: 10;
		bottom: calc(0rpx + var(--window-bottom));
		background-size: 100% auto !important;
	}

	.codeLogin_fd1_0 {
		border-bottom: 1px solid #eee;
		height: 125rpx;
	}

	.codeLogin_fd1_0_c0 {
		width: 34rpx;
		height: 34rpx;
		margin: 0rpx 28rpx 0rpx 0rpx;
	}

	.codeLogin_fd1_0_c1 {
		line-height: 45rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: #333;
	}

	.codeLogin_fd1_1 {
		border-bottom: 1px solid #eee;
		height: 125rpx;
	}

	.codeLogin_fd1_1_c0 {
		width: 34rpx;
		height: 34rpx;
		margin: 0rpx 28rpx 0rpx 0rpx;
	}

	.codeLogin_fd1_1_c1 {
		line-height: 45rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: #333;
	}

	.codeLogin_fd1_1_c2 {
		line-height: 40rpx;
		font-size: 28rpx;
		color: #1593FF;
		font-weight: 500;
	}

	.codeLogin_fd1_2 {
		line-height: 40rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #333333;
		margin: 32rpx 0rpx 48rpx 0rpx;
	}

	.codeLogin_fd1_3 {
		background: rgba(21, 147, 255, 1);
		background-size: 100% !important;
		border-radius: 44rpx 44rpx 44rpx 44rpx;
		font-size: 32rpx;
		color: #fff;
		line-height: 88rpx;
		font-weight: 500;
		padding: 0rpx 287rpx 0rpx 287rpx;
	}

	.codeLogin_fd1_4 {
		line-height: 40rpx;
		margin: 32rpx 0rpx 0rpx 0rpx;
	}

	.codeLogin_fd1_4_c0 {
		font-size: 28rpx;
		font-weight: 400;
		color: #999999;
	}

	.codeLogin_fd1_4_c1 {
		background: rgba(153, 153, 153, 1);
		background-size: 100% !important;
		width: 1rpx;
		height: 24rpx;
		background-size: 100% auto !important;
		margin: 0rpx 32rpx 0rpx 32rpx;
	}

	.codeLogin_fd1_4_c2 {
		font-size: 28rpx;
		font-weight: 400;
		color: #999999;
	}

	.codeLogin_flex_1 {
		padding: 0rpx 56rpx 0rpx 56rpx;
	}

	.codeLogin_fd0_0 {
		background: transparent;
		background-size: 100% !important;
		width: 180rpx;
		height: 180rpx;
		border-radius: 48rpx 48rpx 48rpx 48rpx;
		background-size: 100% auto !important;
	}

	.codeLogin_flex_0 {
		padding: 110rpx 0rpx 80rpx 0rpx;
	}

	.page {
		min-height: calc(100vh - var(--window-bottom));
		background: url(https://app.ezsdcsm.com/images/1.png) no-repeat, #fff;
		background-size: 100% auto !important;
	}
</style>
